package com.topinfo.etl.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbUtil {
    public static String getSelectTableColListSql(String dbType) {
        if ("MySQL".equals(dbType)) {
            return "SELECT * FROM information_schema.COLUMNS c WHERE c.TABLE_SCHEMA = ? AND c.TABLE_NAME = ?";
        } else if ("Oracle".equals(dbType)) {
            return "";
        } else if ("DB2".equals(dbType)) {
            return "";
        } else if ("Sql Server".equals(dbType)) {
            return "";
        }
        return "";
    }

    public static String getSelectTableListSql(String dbType) {
        if ("MySQL".equals(dbType)) {
            return "select t.TABLE_NAME as tableName FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA = ?";
        } else if ("Oracle".equals(dbType)) {
            return "";
        } else if ("DB2".equals(dbType)) {
            return "";
        } else if ("Sql Server".equals(dbType)) {
            return "";
        }
        return "";
    }

    public static Connection getDbConn(String driver, String dbUrl, String user, String password) {
        Connection conn = null;
        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(dbUrl, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

    public static String getDbUrl(String type, String serverAddr, String port, String name) {
        if ("MySQL".equals(type)) {
            return "jdbc:log4jdbc:mysql://"+serverAddr+":"+port+"/"+name;
        } else if ("Oracle".equals(type)) {
            return "jdbc:log4jdbc:oracle:thin:@"+serverAddr+":"+port+":"+name;
        } else if ("DB2".equals(type)) {
            return "jdbc:log4jdbc:db2://"+serverAddr+":"+port+"/"+name;
        } else if ("Sql Server".equals(type)) {
            return "jdbc:log4jdbc:sqlserver://"+serverAddr+":<port>;databaseName="+name;
        }
        return "";
    }

    public static String getDriverClassName(String type) {
        if ("MySQL".equals(type)) {
            return "com.mysql.jdbc.Driver";
        } else if ("Oracle".equals(type)) {
            return "Oracle.jdbc.driver.OracleDriver";
        } else if ("DB2".equals(type)) {
            return "com.ibm.db2.jcc.DB2Driver";
        } else if ("Sql Server".equals(type)) {
            return "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        }
        return "";
    }
}
